//
//  ButtonFormControl.m
//  iCodeMac
//
//  Created by Andrew Barry on 29/03/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "ButtonFormControl.h"
#import "JavascriptForm.h"

@implementation ButtonFormControl
-(id) valueForProperty: (NSString *) name {
	if ([name isEqualToString:@"caption"]) {
		return [(NSButton *) view title];
	} else if ([name isEqualToString:@"shortcut"]) {
		return [NSNumber numberWithInt:shortcut];
	} else if ([name isEqualToString:@"checked"]) {
		return [NSNumber numberWithInt:checked];
	} else if ([name isEqualToString:@"dataSource"]) {
		return dataSource;
	} else if ([name isEqualToString:@"dataField"]) {
		return dataField;
	} else if ([name isEqualToString:@"value"]) {
		return value;
	}
	return [super valueForProperty: name];
}

-(void) updateShortcutForButton: (NSButton *) btn {
	if (shortcut == 1)
		[btn setKeyEquivalent:@"\r"];
	else if (shortcut == 2)
		[btn setKeyEquivalent:@"\e"];
	else 
		[btn setKeyEquivalent:@""];
}

-(void) setValue: (id) val forProperty:(NSString*) name {
	if ([name isEqualToString:@"caption"]) {
		[(NSButton *) view setTitle: (NSString *) val];
	} else if ([name isEqualToString:@"shortcut"]) {
		shortcut = [val intValue];
		[self updateShortcutForButton: (NSButton *) view];
	} else if ([name isEqualToString:@"checked"]) {
		checked = [val intValue];
		[(NSButton *) view setIntValue:checked];
	} else if ([name isEqualToString:@"dataSource"]) {
		dataSource = val;
	} else if ([name isEqualToString:@"dataField"]) {
		[dataField autorelease];
		dataField = [val retain];
	} else if ([name isEqualToString:@"value"]) {
		[value autorelease];
		value = [val retain];
	} else {
		[super setValue: val forProperty: name];
	}
}

-(NSArray *) getEvents {
	return [NSArray arrayWithObjects:@"Click", nil];
}

-(void) saveFields: (NSMutableDictionary *) content {
	[content setObject: [(NSButton *) view title] forKey: @"caption"];
	[content setObject: [NSNumber numberWithInt:shortcut] forKey: @"shortcut"];
	if ([controlType isEqualToString:@"checkbox"]) {
		[content setObject: [NSNumber numberWithInt:checked] forKey: @"checked"];
		if (dataSource != nil && dataSource != [NSNull null]) {
			[content setObject: [dataSource controlName] forKey: @"dataSource"];
		}
		if (dataField)
			[self saveField: content forKey: @"dataField"];
	}
	if ([controlType isEqualToString:@"radio"]) {
		if (value)
			[content setObject: value forKey: @"value"];
	}
	[super saveFields: content];
}

-(void) postLoad {
	FormEditor *editor = [formDocument editor];
	if ([dataSourceLoadName length] > 0) {
		dataSource = [editor controlByName:dataSourceLoadName];
	}
	
	[super postLoad];
}

-(NSView *) load: (NSDictionary *) defn withFrame: (NSRect) frame {
	NSString *caption = [defn objectForKey:@"caption"];
	if (!caption)
		caption = controlType;
	shortcut = [[defn objectForKey:@"shortcut"] intValue];
	checked = [[defn objectForKey:@"checked"] intValue];
	dataSourceLoadName = [defn objectForKey:@"dataSource"];
	dataField = [defn objectForKey:@"dataField"];
	value = [defn objectForKey:@"value"];

	if ([controlType isEqualToString:@"checkbox"]) {
		NSButton *btn = [[NSButton alloc] initWithFrame:frame];
		[btn setButtonType:NSSwitchButton];
		[btn setTitle:caption];
		[btn setIntValue:checked];
		return btn;
	} else if ([controlType isEqualToString:@"radio"]) {
		NSButton *btn = [[NSButton alloc] initWithFrame:frame];
		[btn setButtonType:NSRadioButton];
		[btn setTitle:caption];
		return btn;
	} else {
		NSButton *btn = [[NSButton alloc] initWithFrame:frame];
		[btn setButtonType:NSMomentaryPushInButton];
		[btn setBezelStyle:NSRoundedBezelStyle];
		[btn setTitle:caption];
		[self updateShortcutForButton: btn];
		return btn;
	}
}

-(NSString *) propsNibName {
	if ([controlType isEqualToString:@"button"]) {
		return @"PushButtonProps";
	} else if ([controlType isEqualToString:@"checkbox"]) {
		return @"CheckboxProps";
	} else if ([controlType isEqualToString:@"radio"]) {
		return @"RadioProps";
	} else {
		return @"ButtonProps";
	}
}

-(NSArray *) autocompletes {
	return [NSArray arrayWithObjects: @"left", @"top", @"width", @"height", @"caption", @"state", @"checked", @"enabled", @"value", nil];
}
@end
